<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class QingtaokeController extends BackendController
{

    protected $api = "http://openapi.qingtaoke.com";
    protected $apiKey = "Nmdmy2SF";
    protected $v = '1.0';

    public function _initialize() {
        parent::_initialize();
        set_time_limit(0);
        $this->assign([
            'apikey' => $this->apiKey,
            'v' => $this->v
        ]);
    }

    /**
     * 热词排行
     */
    public function hotWordes()
    {
        if(IS_POST) {
            $t = I('post.t');
            $url = $this->api.'/hot'.'?'.'app_key='.$this->apiKey.'&v='.$this->v.'&t='.$t;
            $data = file_get_contents($url);
            $datas = json_decode($data);
            $this->assign('datas',$datas);
        }
        $this->display();
    }

    /**
     * 商品列表获取
     */
    public function listGoods()
    {
        if(IS_POST) {
            $page = I('post.page');
            $page_size = I('post.page_size');
            $sort = I('post.sort');
            $cat = I('post.cat');
            $min_price = I('post.min_price');
            $max_price = I('post.max_price');
            $new = I('post.new',0);
            $is_ju = I('post.is_ju',0);
            $is_tqg = I('post.is_tqg',0);
            $is_ali = I('post.is_ali',0);
            $p = 1;
            while($p <= $page){
                $url = $this->api.'/qingsoulist?'.'sort='.$sort.'&page='.$p.'&app_key='.$this->apiKey.'&v='.$this->v.'&cat='.$cat.'&min_price='.$min_price.'&max_price='.$max_price.'&new='.$new.'&is_ju='.$is_ju.'&is_tqg='.$is_tqg.'&is_ali='.$is_ali.'&page_size='.$page_size;
                $data = file_get_contents($url);
                $msg = $this->add($data);
                $p++;
            }
            $msg['p'] = $page;
            $this->assign('msg',$msg);
        }

        $this->display();
    }

    /**
     * 销量爆款
     */
    public function sales()
    {
        if(IS_POST) {
            $url = $this->api.'/baokuan'.'?'.'app_key='.$this->apiKey.'&v='.$this->v;
            $data = file_get_contents($url);
            $msg = $this->add($data,200);
            $this->assign('msg',$msg);
        }
        $this->display();
    }

    /**
     * 商品搜索功能
     */
    public function search()
    {
        if(IS_POST) {
            $s_type = I('post.s_type');
            $key_word = I('post.key_word');

            if(substr($key_word,0,4) == 'http') {
                $key_word = urlencode(I('post.key_word'));
            }
            $page = I('post.page');
            $cat = I('post.cat');
            $min_price = I('post.min_price');
            $max_price = I('post.max_price');
            $sort = I('post.sort');
            $is_ju = I('post.is_ju',0);
            $is_tqg = I('post.is_tqg',0);
            $is_ali = I('post.is_ali',0);
            $p = 1;
            while($p <= $page){
                $url = $this->api.'/search?'.'s_type='.$s_type.'&key_word='.$key_word.'&app_key='.$this->apiKey.'&page='.$p.'&v='.$this->v.'&cat='.$cat.'&min_price='.$min_price.'&max_price='.$max_price.'&sort='.$sort.'&is_ju='.$is_ju.'&is_tqg='.$is_tqg.'&is_ali='.$is_ali;
                $data = file_get_contents($url);
              //  var_dump($data);
                $p++;

            }

        }
        $this->display();
    }

    public function add($info,$code = '')
    {
         $model = D('Items');
         $msg = $model->goods_insert($info,$code);
         return $msg;
    }
}
